﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAGameEngine;
using XNAGameEngine.Input;
using XNAGameEngine.Particles;

namespace XNAGame
{
    public class Smoke : ParticleEmitter, IInputListener
    {
        public Smoke(Game game, SpriteBatch spritebatch, Vector2 position)
            : base(game, spritebatch, position)
        {
            _texturesList.Add(Game.Content.Load<Texture2D>("Content\\Particles\\smoke"));
            _newParticleScale = new Vector2(0.2f, 0.2f);
            GameEngine.GetService<IManagerInput>().ToggleInputListening(true, this);
            _ParticlesEmittedPerSecond = 50;
        }

        public override void Update(GameTime gameTime)
        {
            _delayTimer += (double)gameTime.ElapsedGameTime.Milliseconds / 1000;

            if (_isActive)
            {
                for (; _delayTimer > _timeBetweenEmits; _delayTimer -= _timeBetweenEmits)
                    _particlesList.Add(GenerateNewParticle());
            }

            foreach (Particle particle in _particlesList)
            {
                particle.Update(gameTime);
                if (particle._color.A - 2 <= 0)
                    _deadParticleList.Add(particle);
                else particle._color.A -= 2;
            }

            foreach (Particle particle in _deadParticleList)
                _particlesList.Remove(particle);

            _deadParticleList.Clear();
        }

        protected override Particle GenerateNewParticle()
        {
            _newParticleTexture =
                _texturesList[_random.Next(_texturesList.Count)];

            _newParticleColor = Color.Gray;

            _newParticleTimeToLive = 1 + _random.Next(5);

            _newParticlePosition = _location + new Vector2(
                    (float)_random.NextDouble() * 10, (float)_random.NextDouble() * 10);

            _newParticlePosVelocity = new Vector2(
                (float)(_random.NextDouble() * 2 - 1) / 5,
                (float)(_random.NextDouble()) - 1) / 10;

            _newParticlePosAcceleration = new Vector2(
                (float)(_random.NextDouble() * 2 - 1) / 150,
                (float)-0.01);

            _newParticleAngle = 0;

            _newParticleAngVelocity = 10.5f * (_random.NextDouble() * 10 - 1);

            //_newParticleAngAcceleration = 0.2f * (_random.NextDouble() * 2 - 1);

            _newParticleScale = new Vector2(
                    0.1f * (float)(_random.NextDouble() * 2 - 1),
                    0.1f * (float)(_random.NextDouble() * 2 - 1));

            _newParticleScaVelocity = new Vector2(
                    0.0001f * (float)(_random.NextDouble() * 2 - 1),
                    0.0001f * (float)(_random.NextDouble() * 2 - 1));

            _newParticleScaAcceleration = new Vector2(0.0001f * (float)(_random.NextDouble() * 2 - 1));

            return new Particle(
                _newParticleTexture, _newParticleColor,
                _newParticleTimeToLive, _newParticlePosition,
                _newParticlePosVelocity, _newParticlePosAcceleration,
                _newParticleAngle, _newParticleAngVelocity,
                _newParticleAngAcceleration, _newParticleScale,
                _newParticleScaVelocity, _newParticleScaAcceleration);
        }

        #region IInputCallbacks Members

        public void InputPressed(InputMessage input)
        {
            if (input._mouse == MouseClick.MOVE)
                _location = input._vectorVal.Value;

            if (input._mouse == MouseClick.LEFT)
                _isActive = false;

        }

        public void InputReleased(InputMessage input)
        {
            if (input._mouse == MouseClick.LEFT)
                _isActive = true;
        }

        public void InputHeld(InputMessage input)
        { }

        public event ManagerInput.InputRegisterListenerEventHandler ThrowUnregisterListenerEvent;

        #endregion
    }
}